package com.leo.springbootmultithreadbasic.async.withthreadpool;

import com.leo.springbootmultithreadbasic.buildthread.usethreadpool.ThreadPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Leo
 * @version V1.0
 */
@Service("AsyncTaskService2")
public class AsyncTaskService {

    private static final Logger logger = LoggerFactory.getLogger(AsyncTaskService.class);

    // 若五指定的线程池，则使用SimpleAsyncTaskExecutor
    @Async("commonThreadPool")
    public void test1() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("我是一个异步执行的子任务。执行了 3000 milliseconds");
    }

    @Bean("commonThreadPool")
    public ThreadPoolExecutor commonThreadPool() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolConfig().threadPoolExecutor;
        return threadPoolExecutor;
    }
}
